package com.ziyun.pop.web.modules.storeMaterials.controller;

import com.utility.JsonUtils;
import com.utility.New;
import com.utility.StringUtils;
import com.utility.http.HttpUtils;
import com.ziyun.pop.modules.storeMaterials.entity.MaterialCategoryEntity;
import com.ziyun.pop.modules.storeMaterials.service.MaterialCategoryService;
import io.renren.common.utils.R;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * @Author ZhaoWei
 * @Date 2018/10/18 11:37
 * @Version V1.0
 */

@RestController
@RequestMapping("material/materialcategory")
public class MaterialCategoryController {

	@Autowired
	private MaterialCategoryService materialCategoryService;

	@Value("${erpApi}")
	private String erpApi ;

	@RequestMapping("/select")
	public R select(){
		//查询列表数据
		List<MaterialCategoryEntity> materialCategoryList = materialCategoryService.queryAllList();

		//添加顶级菜单
		MaterialCategoryEntity root = new MaterialCategoryEntity();
		root.setCategoryId(0);
		root.setCategoryName("一级分类");
		root.setParentId(-1);
		root.setOpen(true);
		materialCategoryList.add(root);

		return R.ok().put("materialCategoryList", materialCategoryList);

	}

	/**
	 * 获取分类树信息
	 */
	@RequestMapping("/getTree")
	public R getTree(){
		String url = erpApi + "/material/materialcategory/getTree";
		Map<String, Object> paramHeaderMap = New.hashMap();
		Map<String, Object> paramBodyMap = New.hashMap();
		String response = HttpUtils.doPost(url, paramHeaderMap, JsonUtils.map2String(paramBodyMap));
		if(StringUtils.isNotBlank(response)){
			Map<String, Object> resultMap = com.ziyun.pop.common.utils.JsonUtils.jsonToMap(response);
			return  R.ok().put("result",resultMap);
		}else{
			return  R.error().put("msg","接口调用异常");
		}
	}

}
